package java_1_26.exception;

import java.util.Scanner;

/**
 * 自定义异常：自定义异常实现登录
 */
public class Login {
    public static final String USER="美女";
    public static final String PASSWORD="123456";

    public static void main(String[] args) {
        try{
            login();
            System.out.println("登陆成功");
        }catch (UserNameException e){
            e.printStackTrace();
        }catch (PasswordException e){
            e.printStackTrace();
        }
    }
    public static void login() throws UserNameException{
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String name=scanner.next();
        System.out.println("请输入密码");
        String password=scanner.next();
        if(!name.equals(USER)){
            //用户名错误，抛出用户名错误异常:是个受查异常，必须有try..catch包裹或者throws抛出
               throw new UserNameException("用户名错误");
        }
        if(!password.equals(PASSWORD)){
            //抛出密码错误异常
            throw new PasswordException("密码错误");
        }
    }
}
class UserNameException extends java.lang.Exception{
    //继承受查异常，要进行异常地抛出
    //login()后接throws UserNameException抛出此异常
    public UserNameException(String msg){
        super(msg);
    }
}
class PasswordException extends RuntimeException{
    //这里进行了非受查异常，不需要进行显示异常处理。login() 后无需抛PasswordException
    public PasswordException(String msg){
        super(msg);
    }
}